package com.bd.scm.module.utils.httpClient;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Map;

/**
 * HttpClient工具类
 *
 * @author YH
 * @version 1.0
 * @date 2020/10/15 15:49
 */
public class HttpClientUtil {

    /**
     * 请求编码
     */
    private static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * 执行HTTP POST请求
     *
     * @param url         url
     * @param accessToken 凭证
     * @param param       参数
     * @return
     */
    public static JSONObject httpPostWithJSON(String url, String accessToken, String param) {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        try {
            if (url == null || url.trim().length() == 0) {
                throw new Exception("URL is null");
            }
            HttpPost httpPost = new HttpPost(url);
            client = HttpClients.createDefault();
            if (param != null) {
                StringEntity entity = new StringEntity(param, DEFAULT_CHARSET);//解决中文乱码问题
                entity.setContentEncoding(DEFAULT_CHARSET);
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            if (!"".equals(accessToken)) {
                httpPost.setHeader("Authorization", "bearer " + accessToken);
            }
            HttpResponse resp = client.execute(httpPost);
            if (resp.getStatusLine().getStatusCode() == 200) {
                String s = EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET);
                jsonObject = JSONObject.parseObject(s);
                return jsonObject;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(client);
        }
        return null;
    }

    /**
     * 执行HTTP GET请求
     *
     * @param url         url
     * @param accessToken 凭证
     * @param param       参数
     * @return
     */
    public static JSONObject httpGetWithJSON(String url, String accessToken, Map<String, ?> param) {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        try {
            if (url == null || url.trim().length() == 0) {
                throw new Exception("URL is null");
            }
            client = HttpClients.createDefault();
            if (param != null) {
                StringBuffer sb = new StringBuffer("?");
                for (String key : param.keySet()) {
                    sb.append(key).append("=").append(param.get(key)).append("&");
                }
                url = url.concat(sb.toString());
                url = url.substring(0, url.length() - 1);
            }
            HttpGet httpGet = new HttpGet(url);
            if (!"".equals(accessToken)) {
                httpGet.setHeader("Authorization", "bearer " + accessToken);
            }
            HttpResponse resp = client.execute(httpGet);
            if (resp.getStatusLine().getStatusCode() == 200) {
                String s = EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET);
                jsonObject = JSONObject.parseObject(s);
                return jsonObject;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(client);
        }
        return null;
    }

    /**
     * 关闭HTTP请求
     *
     * @param client
     */
    private static void close(CloseableHttpClient client) {
        if (client == null) {
            return;
        }
        try {
            client.close();
        } catch (Exception e) {
        }
    }

}
